const API = require('../../../utils/api.js'); // 引入封装好的 API 方法
Page({
  data: {
    date: '请选择日期',
    userInfo:{}
  },
  onLoad: function() {
    const app = getApp();
    console.log(app.globalData)
    this.setData({
      userInfo:app.globalData.userInfo
    })
    // 设置默认日期为今天
    const today = new Date()
    const formattedDate = `${today.getFullYear()}-${(today.getMonth() + 1).toString().padStart(2, '0')}-${today.getDate().toString().padStart(2, '0')}`
    this.setData({
      date: formattedDate
    })
  },
  bindDateChange: function(e) {
    this.setData({
      date: e.detail.value
    })
  },
  //formSubmit:async function(e) {
  async formSubmit (e) {
    const formData = e.detail.value
    formData.materialDate = this.data.date;
    // 验证必填字段
    if (!formData.materialId || !formData.materialName || !formData.unit) {
      wx.showToast({
        title: '请填写必填字段',
        icon: 'none'
      })
      return
    }
    console.log(formData)
    let result =await API.materialInfoSave(formData);
    wx.showToast({
      title: '提交成功',
      icon: 'success',
    })
  },

  formReset: function() {
    console.log('表单已重置')
    // 重置日期为今天
    const today = new Date()
    const formattedDate = `${today.getFullYear()}-${(today.getMonth() + 1).toString().padStart(2, '0')}-${today.getDate().toString().padStart(2, '0')}`
    this.setData({
      date: formattedDate
    })
  }
})